package com.example.xjtuse_pc.mobilesafe.TaskManage;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.example.xjtuse_pc.mobilesafe.R;
import com.example.xjtuse_pc.mobilesafe.TaskManageActivity;
import com.example.xjtuse_pc.services.TaskClearService;
import com.example.xjtuse_pc.utils.SeviceUtils;

public class TaskSettingActivity extends Activity {
    private CheckBox cb_show_system, cb_ontime_kill;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_setting);
        sp = getSharedPreferences("config", MODE_PRIVATE);

        cb_ontime_kill = (CheckBox) findViewById(R.id.cb_ontime_kill);
        cb_show_system = (CheckBox) findViewById(R.id.cb_show_system);
        cb_show_system.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor ed = sp.edit();
                if (isChecked) {
                    ed.putBoolean("System_status", true);
                } else {
                    ed.putBoolean("System_status", false);
                }
                ed.commit();
            }
        });
        cb_ontime_kill.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //锁屏是特殊广播时间 清单文件注册不生效  必须代码注册
                Intent i = new Intent(TaskSettingActivity.this, TaskClearService.class);

                if (isChecked) {
                    startService(i);
                    Log.i("join", "cb_ontime_kill");
                } else {
                    stopService(i);
                }
            }
        });
    }

    @Override
    protected void onStart() {
         cb_show_system.setChecked(sp.getBoolean("System_status",false));
           cb_ontime_kill.setChecked(SeviceUtils.isServiceRunning(this,"com.example.xjtuse_pc.services.TaskClearService"));
        super.onStart();
    }
}
